package cn.edu.stu.syllabus.utils;

import android.app.Activity;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.support.annotation.IdRes;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.getkeepsafe.taptargetview.TapTarget;
import com.getkeepsafe.taptargetview.TapTargetSequence;

import cn.edu.stu.syllabus.R;
import cn.edu.stu.syllabus.base.BaseActivity;
import cn.edu.stu.syllabus.datas.ThemeStyle;

/**
 * Created by daidaijie on 2017/3/26.
 * 封装对应的导航处理
 */

public class GuideHelper {

    /**
     * 将TapTargetSequence.Listener包装为抽象类
     */
    public abstract static class OnSequenceAdapter implements TapTargetSequence.Listener {
        @Override
        public void onSequenceFinish() {
        }

        @Override
        public void onSequenceStep(TapTarget lastTarget) {
        }

        @Override
        public void onSequenceCanceled(TapTarget lastTarget) {
        }
    }

    // 对Toolbar上的类菜单自定义item进行导航
    public static TapTarget getToolbarItemTapTarget(Toolbar toolbar, @IdRes int menuId,
                                                    String title, String desc) {
        return TapTarget.forToolbarMenuItem(toolbar, menuId, title, desc)
                .dimColor(android.R.color.black)
                .outerCircleColor(ThemeStyle.getCurrentColorPrimary())
                .titleTextSize(20)
                .descriptionTextSize(16)
                .titleTextColor(android.R.color.white)
                .descriptionTextColor(R.color.colorTextPrimaryWhite)
                .transparentTarget(true)
                .drawShadow(true);
    }

    // 对Toolbar上的伪菜单Item自定义item进行导航
    public static TapTarget getToolbarItemViewTapTarget(View view,
                                                        String title, String desc) {
        return TapTarget.forView(view, title, desc)
                .dimColor(android.R.color.black)
                .outerCircleColor(ThemeStyle.getCurrentColorPrimary())
                .titleTextSize(20)
                .descriptionTextSize(16)
                .titleTextColor(android.R.color.white)
                .descriptionTextColor(R.color.colorTextPrimaryWhite)
                .transparentTarget(true)
                .drawShadow(true);
    }

    // 对Fab进行导航
    public static TapTarget getFabTapTarget(FloatingActionButton fab,
                                            String title, String desc) {
        return TapTarget.forView(fab, title, desc)
                .dimColor(android.R.color.black)
                .outerCircleColor(R.color.colorAccent)
                .targetCircleColor(android.R.color.white)
                .titleTextSize(20)
                .descriptionTextSize(16)
                .tintTarget(false)
                .titleTextColor(android.R.color.white)
                .descriptionTextColor(R.color.colorTextPrimaryWhite)
                .drawShadow(true);
    }

    // 对Toolbar的NavigationView进行导航
    public static TapTarget getNavTapTarget(Toolbar toolbar,
                                            String title, String desc) {
        return TapTarget.forToolbarNavigationIcon(toolbar, title, desc)
                .dimColor(android.R.color.black)
                .outerCircleColor(ThemeStyle.getCurrentColorPrimary())
                .titleTextSize(20)
                .descriptionTextSize(16)
                .titleTextColor(android.R.color.white)
                .descriptionTextColor(R.color.colorTextPrimaryWhite)
                .transparentTarget(true)
                .drawShadow(true);
    }

    // 对TapTargetSequence进行封装，并判断该Activity是否已经导航
    public static void startActSeqNow(final Activity activity, TapTarget... tapTargets) {
        final String activityName = activity.getClass().getName();

        // 需要进行导航的时候，才开始导航
        if (GuideSaver.getInstance().isNeedGuide(activityName)) {
            TapTargetSequence sequence = new TapTargetSequence(activity)
                    .targets(tapTargets);
            sequence.considerOuterCircleCanceled(true);
            sequence.continueOnCancel(true);
            sequence.listener(new OnSequenceAdapter() {
                @Override
                public void onSequenceFinish() {
                    GuideSaver.getInstance().setFinishGuide(activityName);
                }
            });
            sequence.start();
        }
    }

    // 对某个Activity进行解释的封装
    public static TapTarget getActMainTapTarget(@DrawableRes int drawableIcon, BaseActivity activity,
                                                String title, String desc) {
        final Drawable icon = ContextCompat.getDrawable(activity,
                drawableIcon);

        final Rect iconTarget = new Rect(0, 0, icon.getIntrinsicWidth() * 2, icon.getIntrinsicHeight() * 2);

        iconTarget.offset(activity.getDeviceWidth() / 2, activity.getDeviceHeight() / 2);

        return TapTarget.forBounds(iconTarget,
                title, desc)
                .icon(icon)
                .dimColor(R.color.material_black)
                .targetCircleColor(R.color.material_white)
                .outerCircleColor(ThemeStyle.getCurrentColorPrimary())
                .titleTextSize(20)
                .tintTarget(true)
                .descriptionTextSize(16)
                .titleTextColor(android.R.color.white)
                .descriptionTextColor(R.color.colorTextPrimaryWhite)
                .drawShadow(true);
    }

}
